﻿using System;
using System.ComponentModel;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LightController.Frm
{
    public partial class Dlg_LightControllerDebug : Form
    {
        [NonSerialized]
        private ILightController _lightController;
        public ILightController LightController
        {
            get
            {
                return _lightController;
            }
            set
            {
                _lightController = value;
            }
        }

        [NonSerialized]
        private int _chs = 4;
        [NonSerialized]
        string _name;
        [NonSerialized]
        private string _comAddress;

        public int Chs
        {
            get
            {
                return _chs;
            }
        }
        public string ControllerName
        {
            get
            {
                return _name;
            }
        }

        public string ComAddress
        {
            get
            {
                return _comAddress;
            }
        }
        public Dlg_LightControllerDebug()
        {
            InitializeComponent();

        }

        private void trackBarLight_Scroll(object sender, EventArgs e)
        {
            int lightVal = trackBarLight.Value;
            try
            {
                LightController.SetIntensity((int)num_CH.Value, lightVal);
            }
            catch { }
        }

        private void trackBarLight_ValueChanged(object sender, EventArgs e)
        {
            tb_LightValue.Text = trackBarLight.Value.ToString();
        }

        private void tb_LightValue_ValueChanged(object sender, EventArgs e)
        {
            trackBarLight.Value = int.Parse(tb_LightValue.Value.ToString().Trim());
        }

        private void btn_Connect_Click(object sender, EventArgs e)
        {
            Task.Run(() =>
            {
                try
                {
                    LightController.Connect();
                }
                catch { }
            });
        }

        private void btn_Disconnect_Click(object sender, EventArgs e)
        {
            try
            {
                LightController.Disconnect();
            }
            catch { }
        }

        private void btn_OpenAll_Click(object sender, EventArgs e)
        {
            try
            {
                LightController.OpenAllLight();
            }
            catch { }
        }

        private void btn_CloseAll_Click(object sender, EventArgs e)
        {
            try
            {
                LightController.CloseAllLight();
            }
            catch { }
        }

        private void Dlg_LightControllerDebug_Load(object sender, EventArgs e)
        {
            this.Text = LightController.Name + $"[{LightController.CommunicationAddress}]";
            tb_Name.Text = LightController.Name;
            tb_CommunicationAddress.Text = LightController.CommunicationAddress;
            num_LightCHs.Value = LightController.ChanelCount;
            LightController.PropertyChanged += LightController_PropertyChanged;
        }

        private void LightController_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            tb_Status.Text = e.PropertyName;
        }

        private void btn_Save_Click(object sender, EventArgs e)
        {
            string com = tb_CommunicationAddress.Text.Trim();
            if (com.Contains("."))
            {
                string[] ips = com.Split('.');

                byte[] ipAddress = new byte[4];
                if (!byte.TryParse(ips[0], out ipAddress[0]) ||
                    !byte.TryParse(ips[1], out ipAddress[1]) ||
                    !byte.TryParse(ips[2], out ipAddress[2]) ||
                    !byte.TryParse(ips[3], out ipAddress[3]))
                {
                    MessageBox.Show("IP地址格式不正确！");
                    return;
                }
            }
            else if (com.ToLower().Contains("com"))
            {
                string[] c = com.ToUpper().Split('M');
                int comPort = 0;
                if (!int.TryParse(c[1], out comPort))
                {
                    MessageBox.Show("串口格式不正确！");
                    return;
                }
            }
            else
            {
                MessageBox.Show("通讯地址格式不正确！");
                return;
            }
            _name = tb_Name.Text;
            _chs = (int)num_LightCHs.Value;
            _comAddress = tb_CommunicationAddress.Text;



            LightController.PropertyChanged -= LightController_PropertyChanged;
            this.DialogResult = DialogResult.OK;
        }

        private void Dlg_LightControllerDebug_FormClosing(object sender, FormClosingEventArgs e)
        {
            LightController.PropertyChanged -= LightController_PropertyChanged;
        }

    }
}
